package com.yingli.myapplication.test;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yingli.myapplication.R;

import java.util.List;

public class LivingAdapter extends RecyclerView.Adapter<Living> {
    private Context context;
    private List<Entity> entity;

    public LivingAdapter(Context context, List<Entity> entity) {
        this.entity = entity;
        this.context = context;
    }

    @NonNull
    @Override
    public Living onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.avtivty_test,parent,false);
        return new Living(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Living holder, int position) {
        Entity en = entity.get(position);
        holder.Name.setText(en.getName());
        holder.Content.setText(en.getContent());

//        holder.itemView.setOnClickListener(v -> {
//            entity.remove(position);
//            notifyItemRemoved(position);
//            //插入多条后，提示更新
////            notifyItemRangeInserted();
//            //删除多条后，提示更新
////            notifyItemRangeRemoved();
//            //通知删除以后，要通知列表项更新
//            notifyItemRangeChanged(position,entity.size()-position);
//        });
        holder.itemView.setOnLongClickListener(v -> {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);

            builder.setMessage("是否删除");
            builder.setTitle("请注意");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    entity.remove(builder);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position,entity.size()-position);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.show();

            return false;
        }); 

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return entity.size();
    }

}
